package com.dnjn.happiness.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.dnjn.happiness.cms.domain.Article;
import com.dnjn.happiness.cms.service.IArticleService;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import com.dnjn.happiness.common.cloud.controller.BaseController;

import com.dnjn.happiness.cms.service.IBannerService;
import com.dnjn.happiness.cms.domain.Banner;

/**
 * <p>
 * Banner 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-10-10
 */
@Tag(name = "cms Banner Banner", description = "Banner")
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {


    @Autowired
    protected IBannerService baseService;

    @Autowired
    private IArticleService articleService;

    @GetMapping("/list")
    public AjaxResult list() {
        List<Banner> list = baseService.lambdaQuery().eq(Banner::getShowable, "Y").eq(Banner::getDeleted, "0").orderByDesc(Banner::getBannerId).list();
        return AjaxResult.success(list);
    }
    @RequiresPermissions("cms:banner:query")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }
    @RequiresPermissions("cms:banner:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Banner domain) {

        validLinkedUrl(domain.getCoverUrl());
        domain.setCreateBy(SecurityUtils.getUsername());
        return toAjax(baseService.save(domain));
    }
    @RequiresPermissions("cms:banner:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Banner domain) {
        validLinkedUrl(domain.getCoverUrl());

        return toAjax(baseService.lambdaUpdate().set(Banner::getArticleId, domain.getArticleId()).set(Banner::getArticleTitle, domain.getArticleTitle())
                .eq(Banner::getBannerId, domain.getBannerId()).update(domain));
    }
    @RequiresPermissions("cms:banner:remove")
    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        for (Long bannerId : pkIds) {
            Banner banner = baseService.getById(bannerId);
            Long articleId = banner.getArticleId();
            if (null != articleId) {
                Article article = articleService.getById(articleId);
                if (article.getDeleted().equals("1")) {
                    baseService.deleteBanner(bannerId);
                } else {
                    return error("请先删除关联文章信息");
                }
            } else {
                baseService.deleteBanner(bannerId);
            }

        }
        return success();
    }
    @RequiresPermissions("cms:banner:list")
    @PostMapping("/page")
    public AjaxResult page(@RequestBody Banner domain, @Parameter(required = true)@RequestParam(defaultValue = "false") Boolean fromFront, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size).addOrder(OrderItem.desc("banner_id"));
        QueryWrapper<Banner> wrapper =  new QueryWrapper<Banner>()
                .eq(StringUtils.isNotEmpty(domain.getShowable()), "showable", domain.getShowable())
                .eq(StringUtils.isNotEmpty(domain.getGotoFlag()), "goto_flag", domain.getGotoFlag())
                .eq(StringUtils.isNotEmpty(domain.getDeleted()), "deleted", domain.getDeleted())
                .like(StringUtils.isNotEmpty(domain.getTopic()), "topic", domain.getTopic());

        if(fromFront){
            wrapper
                    .eq("showable", "Y").eq("deleted", "0");
        }
        Page<Banner> list = baseService.page(page, wrapper);
        return success(list);
    }
    @RequiresPermissions("cms:banner:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Banner domain) {
        List<Banner> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        util.exportExcel(response, list, "参数数据");
    }


}